/**********************************************
 * 
 * File Name				:	ParametersServiceImpl.java
 * 
 * Last Changed Date		: 	$Date: 2009-08-06 14:51:00 +0800 (星期四, 06 八月 2009) $
 * 
 * Last Changed Revision	:	$Revision: 92 $
 * 
 * Last Changed Author		:	$Author: cfanboy $
 * 
 * File Header URL			:	$HeadURL: svn://www.svnhost.cn/Busi-Building/code/branch/src/com/busibuilding/parameters/services/ParametersServiceImpl.java $
 * 
 * ********************************************
 */

package com.busibuilding.parameters.services;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.busibuilding.common.config.sqlmap.entities.ParameterVO;
import com.busibuilding.common.exceptions.DAOException;
import com.busibuilding.common.exceptions.ServiceException;
import com.busibuilding.common.services.CommonServicesImpl;
import com.busibuilding.common.vo.ICommonVO;
import com.busibuilding.parameters.dao.ParametersDAO;

/**
 * Class Name: ParametersServiceImpl.java
 * Description: 
 * @author YU Zhipeng
 * @version 1.0
 * @since 1.0
 * Date: 2009-7-25
 */
@Transactional(rollbackForClassName={"Exception"})
public class ParametersServiceImpl extends CommonServicesImpl implements
		ParametersService {

	private static Logger logger = LogManager.getLogger(ParametersServiceImpl.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4335641708000520482L;
	
	private ParametersDAO dao;
	

	public void setDao(ParametersDAO dao) {
		this.dao = dao;
	}

	@Override
	public int deleteParameter(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.delete(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_DELETE, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_DELETE, e);
		}
		return id;
	}

	@Override
	public String getParamValue(String paramCode, String codeCategory) throws ServiceException {
		String paramValue = EMPUT_STRING;
		List<ParameterVO> resultList = new ArrayList<ParameterVO>();
		try {
			ParameterVO param = new ParameterVO();
			param.setParameterCode(paramCode);
			param.setParameterCategory(codeCategory);
			resultList = dao.searchParameter(param);
			if (resultList.size() > 0){
				ParameterVO vo = resultList.get(0);
				paramValue = vo.getParameterValue();
			}
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_SEARCH, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_SEARCH, e);
		}
		return paramValue;
	}

	@Override
	public int insertParameter(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.insert(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_CREATE, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_CREATE, e);
		}
		return id;
	}

	@Override
	public List<ParameterVO> searchParameterByCondition(ICommonVO vo)
			throws ServiceException {
		List<ParameterVO> resultList = new ArrayList<ParameterVO>();
		try {
			resultList = dao.searchParameter(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_SEARCH, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_SEARCH, e);
		}
		return resultList;
	}

	@Override
	public int updateParameter(ICommonVO vo) throws ServiceException {
		int id = SQLMAP_COMMON_RESULT_ERROR;
		try {
			id = dao.update(vo);
		} catch (DAOException e) {
			logger.fatal(e.getErrorCode() + ":" + e.getErrorMessage());
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_UPDATE, e);
		} catch (Exception e) {
			logger.fatal(e);
			throw new ServiceException(SERVICE_ERROR_CODE_PARAMETER_UPDATE, e);
		}
		return id;
	}

	@Override
	public int getRecordsCount(ICommonVO vo) throws ServiceException {
		// TODO Auto-generated method stub
		return 0;
	}

}
